#pragma once
#include <string>
#include <vector>
#include <format>
#include <data/db_connection_options.hpp>
#include <libpq-fe.h>
#include <data/data_reader.hpp>

namespace blitz_query_cpp::sql
{
    class postgresql_data_reader :public data_reader
    {
        std::string error_msg;
        PGconn *connection = nullptr;
        PGresult *result = nullptr;
        int row_count = 0;
        int current_row = -1;

        template <class... Args>
        bool report_error(std::string_view fmt, Args &&...args)
        {
            error_msg = std::vformat(fmt, std::make_format_args(args...));
            return false;
        }

    public:
        const std::string &get_error_msg() { return error_msg; }
        postgresql_data_reader();
        postgresql_data_reader(const db_connection_options &options);
        bool connect(const db_connection_options &options);
        ~postgresql_data_reader();
        bool execute_sql(std::string_view sql, const std::vector<const char *> &params);
        virtual bool next_row();
        virtual std::string_view read_value(int column);
    };
}